/**
 * Copyright (c) 2005-2011 springside.org.cn
 * 
 * $Id: PropertiesLoader.java 1690 2012-02-22 13:42:00Z calvinxiu $
 */
package com.thinkgem.jeesite.common.utils;

import com.thinkgem.jeesite.common.config.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/**
 * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值，但以System的Property优先.
 * @author calvin
 * @version 2013-05-15
 */
public class PropertiesWriter{

	private static Logger logger = LoggerFactory.getLogger(PropertiesWriter.class);
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();
	private static Resource resource = null;
    private static Properties properties;
	public PropertiesWriter(String resourcesPaths){
		resource = resourceLoader.getResource(resourcesPaths);
	}

    public static void setProperties(Properties param){
        properties = param;
    }

    /**
	 * 载入多个文件, 文件路径使用Spring Resource格式.
	 */
	public void writeProperties(String key, String value){
		if(null == properties){
            properties = new Properties();
            properties.setProperty("licenseId", Global.getConfig("licenseId"));
        }
		try{
			OutputStream fos = new FileOutputStream(resource.getFile());
            properties.setProperty(key, value);
            properties.store(fos, "The New properties file");
			fos.close();
		}
		catch(IOException e){
			logger.info("updating " + "ddd" + " value error");
		}
	}
}
